# -*- coding: utf-8 -*-
'''
Created on Apr 25, 2012

@author: LONG HOANG GIANG
'''
import sys
import os
sys.path.append(os.path.expanduser('/home5/vietcntt/longhoanggiang/python'))
sys.path.append(os.path.join(os.path.dirname(__file__), '../'))
from CrawlerLib import commonlib, Http
from urlparse import urljoin
import workerpool
import gzip
import simplejson as json
import threading

class Task(workerpool.Job):
    
    def __init__(self, name, url, func):
        workerpool.Job.__init__(self)
        self.name = name
        self.url = url
        self.callback = func
    
    def run(self):
        self.callback(self.url, self.name)


def getChapter(url, name):
    print 'getChapter {0} : {1}'.format(name, url)
    html = Http.getHtml(url)
    print html
    html = commonlib.convertHTMLEntitiesToUnicode(html)
    tree = commonlib.build_tree_from_html(html)
#    tree = Http.getXMLTree(url)
    if tree == None:
        print 'ERROR: can\'t build tree' 
        return
    images = []
    for item in tree.xpath("//div[@class='itemBody']/div[@class='itemFullText']//img"):
        src = commonlib.normalize_str(item.get('src', '')).strip()
        if src == '': continue
        src = urljoin(url, src)
        print src
        images.append(src)
    
    if len(images) > 0:
        lock = threading.RLock()
        lock.acquire()
        try:
            global ssData
            ssData.append({'chapter': name, 'images': images})
        finally:
            lock.release()
    return
    
ssData = []
def getListLink(output):
    
    url = 'http://truyentranhvn.org/c/conan.html'
    html = Http.getHtml(url)
    print html
    html = commonlib.convertHTMLEntitiesToUnicode(html)
    tree = commonlib.build_tree_from_html(html)
#    tree = Http.getXMLTree(url)
    if tree == None:
        print 'ERROR: can\'t build tree' 
        sys.exit(1)
    pool = workerpool.WorkerPool(size=1)
    for item in tree.xpath("//h3[contains(., 'ĐỌC TRUYỆN ONLINE')]/following-sibling::*[1]/li/a".decode('utf-8')):
        name = commonlib.stringify(item)
        link = commonlib.normalize_str(item.get('href', '')).strip()
        if link != '':
            link = urljoin(url, link)
            pool.put(Task(name, link, getChapter))
    pool.shutdown()
    pool.wait()
    if os.path.dirname(output) == '': output = './' + output
    if not os.path.isdir(os.path.dirname(output)): os.makedirs(os.path.dirname(output), 0777)
    fp = gzip.open(output, 'wb')
    if fp:
        fp.write(json.dumps(ssData))
        fp.close()
    return
        
if __name__ == '__main__':
    
    getListLink('conan.data')
    print '>> Finished at {0}'.format(commonlib.getGMT7Time())
    os._exit(1)